import dayjs from "dayjs"
import {get} from "@utils/http.js"


export const getPlanMixin={
  getPlan
}
export const getPlanBehavior=Behavior({
  methods:{
    getPlan
  }
})
async function getPlan(){
  let data=await get("/plan")
  data=data?.map((item)=>{
    item.ts= dayjs(item.ts).add(-8,"h").valueOf()
    return item
  })
  this.setPlanList(data)
  const weekStatus=[]
  for(let i=0;i<7;i++){
    const weekStart=dayjs().startOf("week").add(i,"day")
    const dayStatus=[]
    for(let j=0;j<7;j++){
      const dayStart=weekStart.add(8+2*j,"h")
      const status=[false,false]
      data=data.filter(({ts,activityId}) => {
        const time=dayjs(ts)
        if(time.isAfter(dayStart)&&time.isBefore(dayStart.add(2,"h"))||time.isSame(dayStart)){
          if(!activityId){
            status[0]=true
          }else{
            status[1]=true
          }
        }
        return !(time.isAfter(dayStart)&&time.isBefore(dayStart.add(2,"h")))
      });
      dayStatus.push(status)
    }
    weekStatus.push(dayStatus)
  }
  this.setWeekStatusList(weekStatus)
}